﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
using System.Linq;
using System.Collections.Concurrent;
using System.Reflection;
using System.Net;

namespace SilverAnts.Utilities
{
    /// <summary>
    /// 通用方法
    /// </summary>
    public class Generic
    {
        #region GUID
        public static string GetGuid()
        {
            return Guid.NewGuid().ToString("n");
        }
        #endregion

        #region 金额转大写
        /// <summary>  
        /// 金额转大写 
        /// </summary>   
        /// <param name="lowerMoney"></param>   
        /// <returns></returns>   
        public static string MoneyToChinese(string lowerMoney)
        {
            string functionReturnValue = null;
            bool isNegative = false; // 是否是负数   
            if (lowerMoney.Trim().Substring(0, 1) == "-")
            {
                // 是负数则先转为正数   
                lowerMoney = lowerMoney.Trim().Remove(0, 1);
                isNegative = true;
            }
            string strLower = null;
            string strUpart = null;
            string strUpper = null;
            int iTemp = 0;
            // 保留两位小数 123.489→123.49　　123.4→123.4   
            lowerMoney = Math.Round(double.Parse(lowerMoney), 2).ToString();
            if (lowerMoney.IndexOf(".") > 0)
            {
                if (lowerMoney.IndexOf(".") == lowerMoney.Length - 2)
                {
                    lowerMoney = lowerMoney + "0";
                }
            }
            else
            {
                lowerMoney = lowerMoney + ".00";
            }
            strLower = lowerMoney;
            iTemp = 1;
            strUpper = "";
            while (iTemp <= strLower.Length)
            {
                switch (strLower.Substring(strLower.Length - iTemp, 1))
                {
                    case ".":
                        strUpart = "元";
                        break;
                    case "0":
                        strUpart = "零";
                        break;
                    case "1":
                        strUpart = "壹";
                        break;
                    case "2":
                        strUpart = "贰";
                        break;
                    case "3":
                        strUpart = "叁";
                        break;
                    case "4":
                        strUpart = "肆";
                        break;
                    case "5":
                        strUpart = "伍";
                        break;
                    case "6":
                        strUpart = "陆";
                        break;
                    case "7":
                        strUpart = "柒";
                        break;
                    case "8":
                        strUpart = "捌";
                        break;
                    case "9":
                        strUpart = "玖";
                        break;
                }

                switch (iTemp)
                {
                    case 1:
                        strUpart = strUpart + "分";
                        break;
                    case 2:
                        strUpart = strUpart + "角";
                        break;
                    case 3:
                        strUpart = strUpart + "";
                        break;
                    case 4:
                        strUpart = strUpart + "";
                        break;
                    case 5:
                        strUpart = strUpart + "拾";
                        break;
                    case 6:
                        strUpart = strUpart + "佰";
                        break;
                    case 7:
                        strUpart = strUpart + "仟";
                        break;
                    case 8:
                        strUpart = strUpart + "万";
                        break;
                    case 9:
                        strUpart = strUpart + "拾";
                        break;
                    case 10:
                        strUpart = strUpart + "佰";
                        break;
                    case 11:
                        strUpart = strUpart + "仟";
                        break;
                    case 12:
                        strUpart = strUpart + "亿";
                        break;
                    case 13:
                        strUpart = strUpart + "拾";
                        break;
                    case 14:
                        strUpart = strUpart + "佰";
                        break;
                    case 15:
                        strUpart = strUpart + "仟";
                        break;
                    case 16:
                        strUpart = strUpart + "万";
                        break;
                    default:
                        strUpart = strUpart + "";
                        break;
                }

                strUpper = strUpart + strUpper;
                iTemp = iTemp + 1;
            }

            strUpper = strUpper.Replace("零拾", "零");
            strUpper = strUpper.Replace("零佰", "零");
            strUpper = strUpper.Replace("零仟", "零");
            strUpper = strUpper.Replace("零零零", "零");
            strUpper = strUpper.Replace("零零", "零");
            strUpper = strUpper.Replace("零角零分", "整");
            strUpper = strUpper.Replace("零分", "整");
            strUpper = strUpper.Replace("零角", "零");
            strUpper = strUpper.Replace("零亿零万零元", "亿元");
            strUpper = strUpper.Replace("亿零万零元", "亿元");
            strUpper = strUpper.Replace("零亿零万", "亿");
            strUpper = strUpper.Replace("零万零元", "万元");
            strUpper = strUpper.Replace("零亿", "亿");
            strUpper = strUpper.Replace("零万", "万");
            strUpper = strUpper.Replace("零元", "元");
            strUpper = strUpper.Replace("零零", "零");

            // 对壹元以下的金额的处理   
            if (strUpper.Substring(0, 1) == "元")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "零")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "角")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "分")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "整")
            {
                strUpper = "零元整";
            }
            functionReturnValue = strUpper;

            if (isNegative == true)
            {
                return "负" + functionReturnValue;
            }
            else
            {
                return functionReturnValue;
            }
        }
        #endregion

        #region 比较对象
        /// <summary>
        ///  比较对象改变值
        /// </summary>
        /// <param name="oldEntity"></param>
        /// <param name="newEntity"></param>
        /// <param name="columnNames"></param>
        /// <returns></returns>
        public static string CompareEntityValue(object oldEntity, object newEntity,ConcurrentDictionary<string, string> columnNames)
        {
            if (oldEntity.GetType() != newEntity.GetType())
            {
                return "不是同一个类型的对象";
            }
            Type oldType = oldEntity.GetType();
            Type newType = newEntity.GetType();
            var oldProperties = oldType.GetProperties();
            var newProperties = newType.GetProperties();
            string result = "";
            for (int i = 0; i < oldProperties.Length; i++)
            {
                var oldValue = oldProperties[i].GetValue(oldEntity, null);
                var newValue = newProperties[i].GetValue(newEntity, null);
                if (null == oldValue || null == newValue)
                {
                    continue;
                }
                if (oldValue.ToString() != newValue.ToString())
                {
                    result += string.Format("{0}({1}):[{2}->{3}],", columnNames[oldProperties[i].Name], oldProperties[i].Name,oldValue, newValue);
                }
            }
            return result;
        }
        /// <summary>
        /// 当前对象值
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columnNames"></param>
        /// <returns></returns>
        public static string GetEntityValue(object entity, ConcurrentDictionary<string, string> columnNames)
        {
            string result = "";
            Type type = entity.GetType();
            var properties = type.GetProperties();

            foreach (PropertyInfo p in properties)
            {
                if (p.Name == "ID") { continue; }
                result += string.Format("{0}({1}):{2},", columnNames[p.Name], p.Name, p.GetValue(entity, null));
            }
            return result;
        }
        #endregion

        #region 字符串To对象
        /// <summary>
        /// 将传入值转为对象
        /// </summary>
        /// <param name="paramIds"></param>
        /// <returns></returns>
        public static object ToPrimaryKeyObject(string paramIds)
        {
            if (paramIds.Contains(","))
            {
                return paramIds.Split(',').Select(x => new { ID = Convert.ToInt32(x) });
            }
            else
            {
                return new { ID = Convert.ToInt32(paramIds) };
            }
        }
        #endregion

        #region Excel导出
        public static string CreateExcelByXml(string sheetname, IList data)
        {
            StringBuilder sb_Excel = new StringBuilder();
            sb_Excel.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
            sb_Excel.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
            sb_Excel.Append("xmlns=\"");
            sb_Excel.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
            sb_Excel.Append(" <style>");
            sb_Excel.Append("body");
            sb_Excel.Append(" {mso-style-parent:style0;");
            sb_Excel.Append(" font-family:\"Times New Roman\", serif;");
            sb_Excel.Append(" mso-font-charset:0;");
            sb_Excel.Append(" mso-number-format:\"@\";}");
            sb_Excel.Append("table");
            sb_Excel.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;color:#222; margin:0px;}");
            sb_Excel.Append("thead tr td");
            sb_Excel.Append(" {background-color:#e3e6ea;color:#6e6e6e;text-align:center;font-size:14px;}");
            sb_Excel.Append("tbody tr td");
            sb_Excel.Append(" {font-size:12px;color:#666;}");
            sb_Excel.Append(" </style>");
            sb_Excel.Append(" <xml>");
            sb_Excel.Append(" <x:ExcelWorkbook>");
            sb_Excel.Append(" <x:ExcelWorksheets>");
            sb_Excel.Append(" <x:ExcelWorksheet>");
            //设置工作表 sheet1的名称
            sb_Excel.Append(" <x:Name>" + sheetname + " </x:Name>");
            sb_Excel.Append(" <x:WorksheetOptions>");
            sb_Excel.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>");
            sb_Excel.Append(" <x:Selected/>");
            sb_Excel.Append(" <x:Panes>");
            sb_Excel.Append(" <x:Pane>");
            sb_Excel.Append(" <x:Number>3 </x:Number>");
            sb_Excel.Append(" <x:ActiveCol>1 </x:ActiveCol>");
            sb_Excel.Append(" </x:Pane>");
            sb_Excel.Append(" </x:Panes>");
            sb_Excel.Append(" <x:ProtectContents>False </x:ProtectContents>");
            sb_Excel.Append(" <x:ProtectObjects>False </x:ProtectObjects>");
            sb_Excel.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>");
            sb_Excel.Append(" </x:WorksheetOptions>");
            sb_Excel.Append(" </x:ExcelWorksheet>");
            sb_Excel.Append(" <x:WindowHeight>6750 </x:WindowHeight>");
            sb_Excel.Append(" <x:WindowWidth>10620 </x:WindowWidth>");
            sb_Excel.Append(" <x:WindowTopX>480 </x:WindowTopX>");
            sb_Excel.Append(" <x:WindowTopY>75 </x:WindowTopY>");
            sb_Excel.Append(" <x:ProtectStructure>False </x:ProtectStructure>");
            sb_Excel.Append(" <x:ProtectWindows>False </x:ProtectWindows>");
            sb_Excel.Append(" </x:ExcelWorkbook>");
            sb_Excel.Append(" </xml>");
            sb_Excel.Append("");
            sb_Excel.Append("</head><body>");
            sb_Excel.Append("<table style=\"border-right: 1px solid #CCC;border-bottom: 1px solid #CCC;text-align:center;\">");
            sb_Excel.Append("<tbody>");
            sb_Excel.Append("<tr>");
            sb_Excel.Append("<td>-----------");
            sb_Excel.Append("<td>");
            sb_Excel.Append("</tr>");
            sb_Excel.Append("</tbody></table>");
            sb_Excel.Append("</body></html>");
            return sb_Excel.ToString();
        }

        #endregion

        #region Host IP
        public static string GetHostInfo()
        {
            //得到本机的主机名
            string host = " 主机名：" + Dns.GetHostName();
            //取得本机IP
            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in ipEntry.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    host += "; IP_V4:" + ip.ToString();
                }
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                {
                    host += "; IP_V6:" + ip.ToString();
                }
            }
            return host;
        }
        #endregion

        #region 全角转半角
        /// <summary>
        /// 全角转半角  
        /// 半角空格32,全角空格12288  
        /// 其他字符半角33~126,其他字符全角65281~65374,相差65248  
        /// </summary>  
        /// <param name="input"></param>  
        /// <returns></returns>  
        public static string ConvertSBCToDBC(string input)
        {
            char[] cc = input.ToCharArray();
            for (int i = 0; i < cc.Length; i++)
            {
                if (cc[i] == 12288)
                {
                    // 表示空格  
                    cc[i] = (char)32;
                    continue;
                }
                if (cc[i] > 65280 && cc[i] < 65375)
                    cc[i] = (char)(cc[i] - 65248);
            }
            return new string(cc);
        }
        #endregion
    }

}
